package com.cool.cloud.controller;

import com.cool.cloud.entity.*;
import com.cool.cloud.utils.LogUtils;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.slf4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.*;

/**
 * @ClassName: SystemController
 * @Description: 系统页面跳转控制器
 * @author: laijunji
 * @date 2020/2/25 22:02
 * @Version: 1.0
 **/
@Controller
public class SystemController extends BaseController {

    Logger logger = LogUtils.getInstance(SystemController.class);

    /**
     * @return java.lang.String
     * @Description 前往文件上传页面
     * @Author laijunji
     * @Date 15:16 2020/2/26
     * @Param [fId, fName, map]
     **/
    @GetMapping("/upload")
    public String toUploadPage(Integer fId, String fName, Map<String, Object> map, String status) {
        //包含的子文件夹
        List<FileFolder> folders = null;
        //当前文件夹信息
        FileFolder nowFolder = null;
        //当前文件夹的相对路径
        List<FileFolder> location = new ArrayList<>();
        if (fId == null || fId <= 0) {
            //代表当前为根目录
            fId = 0;
            folders = fileFolderService.getRootFoldersByFileStoreId(loginUser.getFileStoreId());
            nowFolder = FileFolder.builder().fileFolderId(fId).build();
            location.add(nowFolder);
        } else {
            //当前为具体目录
            folders = fileFolderService.getFileFolderByParentFolderId(fId);
            nowFolder = fileFolderService.getFileFolderByFileFolderId(fId);
            //遍历查询当前目录
            FileFolder temp = nowFolder;
            while (temp.getParentFolderId() != 0) {
                temp = fileFolderService.getFileFolderByFileFolderId(temp.getParentFolderId());
                location.add(temp);
            }
        }
        Collections.reverse(location);
        //获得统计信息
        FileStoreStatistics statistics = myFileService.getCountStatistics(loginUser.getFileStoreId());
        map.put("statistics", statistics);
        map.put("folders", folders);
        map.put("nowFolder", nowFolder);
        map.put("location", location);
        map.put("status", status);
        logger.info("网盘页面域中的数据:" + map);
        return "main/upload";
    }

    /**
     * @return java.lang.String
     * @Description 前往我的网盘
     * @Author laijunji
     * @Date 23:28 2020/2/10
     * @Param [fId, fName, error, map]
     **/
    @GetMapping("/file-all")
    public String toFileStorePage(Integer fId, Integer error, Map<String, Object> map, Integer cur) {
        //清除搜索记录
        session.removeAttribute("searchName");
        //判断是否包含错误信息
        if (error != null) {
            if (error == 1) {
                session.setAttribute("tipmsg", "添加失败！当前已存在同名文件夹");
            }
            if (error == 2) {
                session.setAttribute("tipmsg", "重命名失败！文件夹已存在");
            }
        }
        //获取当前查询的页数，如果为空，默认为1
        cur = (cur == null || cur <= 0) ? 1 : cur;
        //分页
        ArrayList<Integer> page = new ArrayList();
        //包含的子文件夹
        List<FileFolder> folders = null;
        //包含的文件
        List<MyFile> files = null;
        //当前文件夹信息
        FileFolder nowFolder = null;
        //当前文件夹的相对路径
        List<FileFolder> location = new ArrayList<>();
        //页面显示为文件夹+文件（若文件夹不超过8个，显示文件夹+文件,若文件夹超过8个,第一页全部显示文件夹/第二页显示文件夹+文件/文件夹小于8则第一页为文件夹＋文件，后面均为文件）
        if (fId == null || fId <= 0) {
            //代表当前为根目录
            fId = 0;
            folders = fileFolderService.getRootFoldersByFileStoreId(loginUser.getFileStoreId());
            files = myFileService.getRootFilesByFileStoreId(loginUser.getFileStoreId());
            nowFolder = FileFolder.builder().fileFolderId(fId).build();
            location.add(nowFolder);
        } else {
            //当前为具体目录,访问的文件夹不是当前登录用户所创建的文件夹
            FileFolder folder = fileFolderService.getFileFolderByFileFolderId(fId);
            if (folder.getFileStoreId() - loginUser.getFileStoreId() != 0) {
                return "redirect:/error401Page";
            }
            //当前为具体目录，访问的文件夹是当前登录用户所创建的文件夹
            folders = fileFolderService.getFileFolderByParentFolderId(fId);
            files = myFileService.getFilesByParentFolderId(fId);
            nowFolder = fileFolderService.getFileFolderByFileFolderId(fId);
            //遍历查询当前目录
            FileFolder temp = nowFolder;
            while (temp.getParentFolderId() != 0) {
                temp = fileFolderService.getFileFolderByFileFolderId(temp.getParentFolderId());
                location.add(temp);
            }
        }
        ArrayList<Object> allObj = new ArrayList<>();
        allObj.addAll(folders);
        allObj.addAll(files);
        int pagenum = (int) Math.ceil(allObj.size() / 8.0);
        page.add(pagenum);
        cur = cur > pagenum ? pagenum : cur;
        page.add(cur);
        folders = new ArrayList<>();
        files = new ArrayList<>();
        List<Object> objects = null;
        if (allObj.size() <= cur * 8 && pagenum > 0) {
            objects = allObj.subList((cur - 1) * 8, allObj.size());
        } else if (allObj.size() > cur * 8 && pagenum > 0) {
            objects = allObj.subList((cur - 1) * 8, (cur - 1) * 8 + 8);
        } else {
            objects = new ArrayList<>();
        }
        for (Object o : objects) {
            if (o instanceof FileFolder) {
                folders.add((FileFolder) o);
            } else {
                files.add((MyFile) o);
            }
        }

        Collections.reverse(location);
        map.put("page", page);
        map.put("permission", fileStoreService.getFileStoreByUserId(loginUser.getUserId()).getPermission());
        map.put("folders", folders);
        map.put("files", files);
        map.put("nowFolder", nowFolder);
        map.put("location", location);
        logger.info("网盘页面域中的数据:" + map);
        return "main/file-all";
    }

    /**
     * @return java.lang.String
     * @Description 前往所有文档页面
     * @Author laijunji
     * @Date 10:26 2020/2/26
     * @Param [map]
     **/
    @GetMapping("/file-doc")
    public String toDocFilePage(Map<String, Object> map, Integer cur) {
        //获取当前查询的页数，如果为空，默认为1
        cur = (cur == null || cur <= 0) ? 1 : cur;
        //分页获得8个文档
        Page<Object> page = PageHelper.startPage(cur, 8);
        List<MyFile> files = myFileService.getFilesByType(loginUser.getFileStoreId(), 1);
        cur = cur > page.getPages() ? page.getPages() : cur;
        map.put("page", page);
        map.put("files", files);
        map.put("permission", fileStoreService.getFileStoreByUserId(loginUser.getUserId()).getPermission());
        return "main/file-doc";
    }

    /**
     * @return java.lang.String
     * @Description 前往所有图像页面
     * @Author laijunji
     * @Date 10:26 2020/2/26
     * @Param [map]
     **/
    @GetMapping("/file-image")
    public String toImageFilePage(Map<String, Object> map, Integer cur) {
        //获取当前查询的页数，如果为空，默认为1
        cur = (cur == null || cur <= 0) ? 1 : cur;
        //分页获得8个图片
        Page<Object> page = PageHelper.startPage(cur, 8);
        List<MyFile> files = myFileService.getFilesByType(loginUser.getFileStoreId(), 2);
        map.put("files", files);
        map.put("page", page);
        map.put("permission", fileStoreService.getFileStoreByUserId(loginUser.getUserId()).getPermission());
        return "main/file-image";
    }

    /**
     * @return java.lang.String
     * @Description 前往所有视频页面
     * @Author laijunji
     * @Date 10:26 2020/2/26
     * @Param [map]
     **/
    @GetMapping("/file-video")
    public String toVideoFilePage(Map<String, Object> map, Integer cur) {
        //获取当前查询的页数，如果为空，默认为1
        cur = (cur == null || cur <= 0) ? 1 : cur;
        //分页获得8个视频
        Page<Object> page = PageHelper.startPage(cur, 8);
        List<MyFile> files = myFileService.getFilesByType(loginUser.getFileStoreId(), 3);
        map.put("files", files);
        map.put("page", page);
        map.put("permission", fileStoreService.getFileStoreByUserId(loginUser.getUserId()).getPermission());
        return "main/file-video";
    }

    /**
     * @return java.lang.String
     * @Description 前往所有音频页面
     * @Author laijunji
     * @Date 10:26 2020/2/26
     * @Param [map]
     **/
    @GetMapping("/file-music")
    public String toMusicFilePage(Map<String, Object> map, Integer cur) {
        //获取当前查询的页数，如果为空，默认为1
        cur = (cur == null || cur <= 0) ? 1 : cur;
        //分页获得8个音乐
        Page<Object> page = PageHelper.startPage(cur, 8);
        List<MyFile> files = myFileService.getFilesByType(loginUser.getFileStoreId(), 4);
        map.put("files", files);
        map.put("page", page);
        map.put("permission", fileStoreService.getFileStoreByUserId(loginUser.getUserId()).getPermission());
        return "main/file-music";
    }

    /**
     * @return java.lang.String
     * @Description 前往其他文件页面
     * @Author laijunji
     * @Date 10:26 2020/2/26
     * @Param [map]
     **/
    @GetMapping("/file-other")
    public String toOtherFilePage(Map<String, Object> map, Integer cur) {
        //获取当前查询的页数，如果为空，默认为1
        cur = (cur == null || cur <= 0) ? 1 : cur;
        //分页获得8个其他文件
        Page<Object> page = PageHelper.startPage(cur, 8);
        List<MyFile> files = myFileService.getFilesByType(loginUser.getFileStoreId(), 5);
        map.put("files", files);
        map.put("page", page);
        map.put("permission", fileStoreService.getFileStoreByUserId(loginUser.getUserId()).getPermission());
        return "main/file-other";
    }

    /**
     * @return java.lang.String
     * @Description 登录之后的用户主页
     * @Author laijunji
     * @Date 10:28 2020/2/26
     * @Param [map]
     **/
    @GetMapping("/index")
    public String index(Map<String, Object> map) {
        //获得统计信息
        FileStoreStatistics statistics = myFileService.getCountStatistics(loginUser.getFileStoreId());
        statistics.setFileStore(fileStoreService.getFileStoreById(loginUser.getFileStoreId()));
        //查询一周统计信息
        FileWeekCount[] count = new FileWeekCount[7];
        for (int i = 1; i <= 7; i++) {
            count[i - 1] = fileWeekCountService.queryOneWeek(i, loginUser.getUserId());
        }
        statistics.setFileWeekCount(count);
        List<SliderPicture> sliderpic = sliderPictureService.queryAll();
        statistics.setSliderPicture(sliderpic);
        map.put("statistics", statistics);
        return "main/index";
    }

    /**
     * @Description 清除错误信息
     * @Author laijunji
     **/
    @GetMapping("/cleanTipMsg")
    public void cleanTipMsg() {
        session.removeAttribute("tipmsg");
        return;
    }

}
